//
//  KSScrollMenuView.m
//  Dingdong
//
//  Created by 程强 on 16/11/17.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "KSScrollMenuView.h"

@interface KSScrollMenuView()
@property (strong, nonatomic) UIScrollView *bgScrollView;
@end

@implementation KSScrollMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:scrollView];
        scrollView.showsHorizontalScrollIndicator = NO;
        self.bgScrollView = scrollView;
    }
    return self;
}
- (void)setItems:(NSArray *)items
{
    _items = items;
    
    CGFloat width = self.bounds.size.width/5.0;
    CGFloat height = self.bounds.size.height;
    
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        KSScrollMenuViewItem *item = (KSScrollMenuViewItem *)obj;
        [item setFrame:CGRectMake(idx*width, 0, width, height)];
        [_bgScrollView addSubview:item];
    }];
    
}
@end


@interface KSScrollMenuViewItem()
@property (strong, nonatomic) UIButton *itemBtn;
@property (strong, nonatomic) KKLabel *itemTitleLbl;
@end

@implementation KSScrollMenuViewItem
- (instancetype)initWithIcon:(NSString *)icon title:(NSString *)title
{
    self = [self init];
    [self.itemBtn setBackgroundImage:LoadImage(icon) forState:UIControlStateNormal];
    self.itemTitleLbl.text = title;
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_itemBtn];
        [_itemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.equalTo(self).offset(-10);
            make.top.equalTo(self).offset(10);
            make.height.equalTo(_itemBtn.mas_width);
        }];
        
        self.itemTitleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Lightning delivery",[UIColor darkGrayColor],[UIFont systemFontOfSize:12.0],NSTextAlignmentLeft);
        }];
        _itemTitleLbl.numberOfLines = 0;
        [self addSubview:_itemTitleLbl];
        [_itemTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_itemBtn);
            make.top.equalTo(_itemBtn.mas_bottom).offset(5.0);
            make.width.equalTo(self).offset(-20);
            make.bottom.equalTo(self).offset(-5);
        }];
        _itemTitleLbl.hidden = YES;
    }
    return self;
}
- (void)setMenuIcon:(NSString *)menuIcon
{
    _menuIcon = menuIcon;
    [self.itemBtn setBackgroundImage:LoadImage(@"menuIcon") forState:UIControlStateNormal];
}
- (void)setMenuName:(NSString *)menuName
{
    _menuName = menuName;
    _itemTitleLbl.text = menuName;
}
@end
